// pages/memorandum/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    myValue: '',
    admin: '0',
    subAdmin: 0,
    userId: '',
    active: 0,
    list: [],
    storageList: [],
    myList: [],
    myStorageList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ admin: wx.getStorageSync('admin'), userId: wx.getStorageSync('userId'), subAdmin: wx.getStorageSync('subAdmin') });
    wx.hideShareMenu();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getMyList();
  },

  getList() {
    wx.showLoading();
    wx.request({
      url: `${app.globalData.baseUrl}/api/memo/selMemoOpenList`,
      method: "GET",
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "application/json"
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({ list: res.data.rows, storageList: res.data.rows });
        }
      },
      complete: () => {
        wx.hideLoading();
      }
    })
  },

  getMyList() {
    wx.showLoading();
    wx.request({
      url: `${app.globalData.baseUrl}/api/memo/selMemoList`,
      method: "GET",
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "application/json"
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({ myList: res.data.rows, myStorageList: res.data.rows });
        }
      },
      complete: () => {
        wx.hideLoading();
      }
    })
  },

  mySearchInput(e) {
    if(!e.detail) {
      this.setData({ myList: this.data.myStorageList });
    } else {
      let myList = [...this.data.myStorageList];
      myList = myList.filter(item => item.title.includes(e.detail));
      this.setData({ myList });
    }
  },

  myClearInput() {
    this.setData({ myList: this.data.storageList });
  },

  
  searchInput(e) {
    if(!e.detail) {
      this.setData({ list: this.data.storageList });
    } else {
      let list = [...this.data.storageList];
      list = list.filter(item => item.title.includes(e.detail));
      this.setData({ list });
    }
  },

  clearInput() {
    this.setData({ list: this.data.storageList });
  },

  linkDetail(e) {
    if(e.currentTarget.dataset.id) {
      wx.navigateTo({
        url: `/pages/editMemorandum/index?id=${e.currentTarget.dataset.id}`,
      })
    } else {
      wx.navigateTo({
        url: `/pages/editMemorandum/index`,
      })
    }
  },

  changeTab(e) {
    this.setData({ active: e.detail.index });
    if(e.detail.index == 0) {
      this.getMyList();
    } else {
      this.getList();
    }
  },

  delete(e) {
    const _that = this;
    wx.showModal({
      content: '确认删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: `${app.globalData.baseUrl}/api/memo/delMemo?id=${e.currentTarget.dataset.id}`,
            method: "GET",
            header: {
              'token': wx.getStorageSync('token'),
              "Content-Type": "application/json"
            },
            success: (res) => {
              if (res.data.code === 0) {
                if(_that.data.active == 0) {
                  _that.getMyList();
                }else {
                  _that.getList();
                }
                wx.showToast({
                  title: '操作成功',
                });
              }
            }
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})